package com.snow.useviewdemo.screenlock;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.snow.common.base.BaseBasesActivity;
import com.snow.common.tool.utils.ToastUtils;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.util.OnAntiDoubleClickListener;

import butterknife.BindView;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-10-14
 * desc   :锁屏功能，唤醒cpu，可以去有道云搜索"一键锁屏"
 */
public class ScreenLockActivity extends BaseBasesActivity {
    @BindView(R.id.btn_01)
    Button btn01;
    @BindView(R.id.btn_02)
    Button btn02;
    DevicePolicyManager dpm;

    @Override
    protected int getContentViewId() {
        return R.layout.act_screen_lock;
    }

    @Override
    protected void initView() {
        dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);

        btn01.setOnClickListener(new OnAntiDoubleClickListener() {
            @Override
            public void onAntiDoubleClick(View v) {
                lock();
                //杀掉进程
//                android.os.Process.killProcess(android.os.Process.myPid())
                new CountDownTimer(10000, 10000) {
                    @Override
                    public void onTick(long millisUntilFinished) {

                    }

                    @Override
                    public void onFinish() {
                        Log.e("snoww", "===openLock====");
                        openLock();
                    }
                }.start();
            }
        });
        btn02.setOnClickListener(new OnAntiDoubleClickListener() {
            @Override
            public void onAntiDoubleClick(View v) {
                ToastUtils.getInstance().showToast("可以去锁屏等待了");
                new CountDownTimer(10000, 10000) {
                    @Override
                    public void onTick(long millisUntilFinished) {

                    }

                    @Override
                    public void onFinish() {
                        Log.e("snoww", "===openLock====");
                        openLock();
                    }
                }.start();
            }
        });


    }

    private void openLock() {
        //通过锁唤醒CPU
        PowerManager manager = (PowerManager) getSystemService(POWER_SERVICE);
        PowerManager.WakeLock mWakeLock = manager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName());
//        PowerManager.WakeLock mWakeLock = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
        if (mWakeLock != null) {
            Log.e("snoww", "===过锁唤醒CPU====");
            mWakeLock.acquire(1000);
        }
    }

    //锁屏
    private void lock() {
        ComponentName componentName = new ComponentName(this, MyDeviceAdminReceiver.class);
        // 判断该组件是否有系统管理员的权限
        boolean isAdminActive = dpm.isAdminActive(componentName);

        if (!isAdminActive) {//这一句一定要有...
            Intent intent = new Intent();
            intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
            startActivity(intent);
        }
        //isAdminActive 为false，直接调用dpm.lockNow()会报java.lang.SecurityException: No active admin owned by uid 10510 for policy #3
        if (isAdminActive) {
            dpm.lockNow();
        }
    }

}
